var sellerCode="";
$(document).ready(function() {
//	top.UppTabs.operator = top.getCurrentTagTitle();// 设置用户tab的标题值
	$( "#sellers" ).autocomplete({
		source: function( request, response ) {
	        $.ajax({
	          url: contextPath + '/iccard/report/getSellers',
	          dataType: "json",
	          data: {
	            maxRows: 10,
	            keyword: request.term
	          },
	          success: function( data ) {
	        	sellerCode="";
	            response( $.map( data, function( item ) {
	              return {
	                label: item.value+','+item.label,
	                value: item.label
	              };
	            }));
	          }
	        });
	      },
	      minLength: 2,
	      select: function( event, ui ) {
		        sellerCode=ui.item.label;
	      },
	      open: function() {
	        $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
	      },
	      close: function() {
	        $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
	      }
	    });
});

function doSearch(){
	var s = $('input[name="beginTime"]').val();
	var e = $('input[name="endTime"]').val();
	if(s == "" || e == "")
		return;
	$("#data_list").datagrid({
		onLoadError : function() {
			uppAjaxError(data);
		},
		onLoadSuccess:function(data){
			$("#export_link").attr('href',contextPath+'/iccard/report/temp/'+data.fileName);
		},
		queryParams : {
			beginTime : s,
			endTime : e,
			sellerCode : sellerCode
		},
		frozenColumns : [ [ {
			field : 'merchantName',
			title : '商户名称',
			width : 80
		}, {
			field : 'merchantCode',
			title : '商户编号',
			width : 80
		}, ] ],
		columns : [ [ {
			field : 'merchantDaliyEarningsDetailId',
			title : '',
			hidden : true
		}, {
			field : 'comboCardExpenseAmount',
			title : '套餐卡消费额'
		}, {
			field : 'comboCardHandleAmount',
			title : '套餐卡应付额'
		}, {
			field : 'comboCardExpectedEarnings',
			title : '套餐卡预期收益额'
		}, {
			field : 'rechargeableCardExpenseAmount',
			title : '充值卡消费额'
		}, {
			field : 'rechargeableCardHandleAmount',
			title : '充值卡应付额'
		}, {
			field : 'rechargeableCardExpectedEarnings',
			title : '充值卡预期收益额'
		}, {
			field : 'expenseTotalAmount',
			title : '消费总额'
		}, {
			field : 'handleTotalAmount',
			title : '应付总额'
		}, {
			field : 'expectedTotalEarnings',
			title : '预期收益总额'
		}, {
			field : 'daliyEarningsDate',
			title : '日期 ',
			align : 'center',
			formatter : function(value, row, index) {
				var d =new Date(Number(value));
				return d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate();
			}
		} ] ],
		autoRowHeight : true,
		fitColumns : true,
		pagination : true,
		nowrap : false,
		singleSelect : true,
		rownumbers : true
	});
	
	paint();
}

function paint(){
	var s = $('input[name="beginTime"]').val();
	var e = $('input[name="endTime"]').val();
	var days=new Array();//日期
	var totals_sales=new Array();//每日销售总额
	var packages_sales=new Array();//套餐卡每日销售
	var prepaids_sales=new Array();//充值卡每日销售
	var packages_sales_total=0.0;//套餐卡累计销售总额
	var prepaids_sales_total=0.0;//充值卡累计销售总额
	
	var totals_profit=new Array();//每日收益总额
	var packages_profit=new Array();//套餐卡每日收益
	var prepaids_profit=new Array();//充值卡每日收益
	var packages_profit_total=0.0;//套餐卡累计收益总额
	var prepaids_profit_total=0.0;//充值卡累计收益总额
    $.ajax({
        url: contextPath + 'iccard/report/bznis/settlement/querychart2',
        dataType: "json",
        async:false,
        data: {
        	beginTime : s,
			endTime : e,
			sellerCode : sellerCode
        },
        success: function( data ) {
        	days = data.days;
        	totals_sales = data.totals_sales;
        	packages_sales = data.packages_sales;
        	prepaids_sales = data.prepaids_sales;
        	packages_sales_total = data.packages_sales_total;
        	prepaids_sales_total = data.prepaids_sales_total;
        	
        	totals_profit = data.totals_profit;
        	packages_profit = data.packages_profit;
        	prepaids_profit = data.prepaids_profit;
        	packages_profit_total = data.packages_profit_total;
        	prepaids_profit_total = data.prepaids_profit_total;
        }
      });
    //
    $('#sales_chart').highcharts({
        chart: {
            type: 'line'
        },
        title: {
            text: '商家销售金额曲线图'
        },
        subtitle: {
            text: '('+days[0]+'至'+days[days.length-1]+')'
        },
        xAxis: {
            categories: days
        },
        yAxis: {
            title: {
                text: '金额（元）'
            }
        },
        tooltip: {
            enabled: true,
            formatter: function() {
                return '<b>'+ this.series.name +'</b><br/>'+
                    this.x +': '+ this.y +'元';
            }
        },
        plotOptions: {
            line: {
                dataLabels: {
                    enabled: true
                },
                enableMouseTracking: true
            }
        },
        series: [{
            name: '总额',
            data: totals_sales
        }, {
            name: '套餐卡消费金额',
            data: packages_sales
        },{
            name: '购物卡消费金额',
            data: prepaids_sales
        }]
    });
//
    $('#profit_chart').highcharts({
        chart: {
            type: 'line'
        },
        title: {
            text: '收益额曲线图'
        },
        subtitle: {
            text: '('+days[0]+'至'+days[days.length-1]+')'
        },
        xAxis: {
            categories: days
        },
        yAxis: {
            title: {
                text: '金额（元）'
            }
        },
        tooltip: {
            enabled: true,
            formatter: function() {
                return '<b>'+ this.series.name +'</b><br/>'+
                    this.x +': '+ this.y +'元';
            }
        },
        plotOptions: {
            line: {
                dataLabels: {
                    enabled: true
                },
                enableMouseTracking: true
            }
        },
        series: [{
            name: '总额',
            data: totals_profit
        }, {
            name: '套餐卡收益金额',
            data: packages_profit
        },{
            name: '购物卡收益金额',
            data: prepaids_profit
        }]
    });

    //饼图
    $('#sales_rate').highcharts({
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
        },
        title: {
            text: '充值卡/套餐卡消费汇总占比情况'
        },
        subtitle: {
            text: '('+days[0]+'至'+days[days.length-1]+')'
        },
        tooltip: {
    	    pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    color: '#000000',
                    connectorColor: '#000000',
                    format: '<b>{point.name}</b>: {point.percentage:.1f} %'
                }
            }
        },
        series: [{
            type: 'pie',
            name: '占比',
            data: [
                ['购物卡消费',   prepaids_sales_total],
                ['套餐看消费',   packages_sales_total]
            ]
        }]
    });
    $('#profit_rate').highcharts({
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
        },
        title: {
            text: '充值卡/套餐卡收益汇总占比情况'
        },
        subtitle: {
            text: '('+days[0]+'至'+days[days.length-1]+')'
        },
        tooltip: {
    	    pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    color: '#000000',
                    connectorColor: '#000000',
                    format: '<b>{point.name}</b>: {point.percentage:.1f} %'
                }
            }
        },
        series: [{
            type: 'pie',
            name: '占比',
            data: [
                ['套餐卡收益',   packages_profit_total],
                ['充值卡收益',   prepaids_profit_total]
            ]
        }]
    });
}